<?php

namespace App\Models\Trip\TripOrderRefund;

use Eloquent;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Validator;

/**
 * 未匹配座位退款记录
 *
 * @property int                       $id
 * @property string                    $user_id   用户id
 * @property string                    $refund_sn 退款单号
 * @property string                    $order_sn  订单单号
 * @property int                       $type      优选铺席 3
 * @property string                    $name      费用名称,过道 靠窗 ,上中下铺,
 * @property int                       $count     未匹配数量
 * @property string                    $amount    总金额
 * @property Carbon|null               $created_at
 * @property Carbon|null               $updated_at
 * @property-read TripOrderRefund|null $refund
 * @method static Builder|NoMatchSeatRefundDetail newModelQuery()
 * @method static Builder|NoMatchSeatRefundDetail newQuery()
 * @method static Builder|NoMatchSeatRefundDetail query()
 * @method static Builder|NoMatchSeatRefundDetail whereAmount($value)
 * @method static Builder|NoMatchSeatRefundDetail whereCount($value)
 * @method static Builder|NoMatchSeatRefundDetail whereCreatedAt($value)
 * @method static Builder|NoMatchSeatRefundDetail whereId($value)
 * @method static Builder|NoMatchSeatRefundDetail whereName($value)
 * @method static Builder|NoMatchSeatRefundDetail whereOrderSn($value)
 * @method static Builder|NoMatchSeatRefundDetail whereRefundSn($value)
 * @method static Builder|NoMatchSeatRefundDetail whereType($value)
 * @method static Builder|NoMatchSeatRefundDetail whereUpdatedAt($value)
 * @method static Builder|NoMatchSeatRefundDetail whereUserId($value)
 * @property string $out_trade_no 关联的支付单号
 * @method static Builder|NoMatchSeatRefundDetail whereOutTradeNo($value)
 * @property int|null $trip_type 出行类型 1 火车 2 飞机 3 酒店
 * @method static Builder|NoMatchSeatRefundDetail whereTripType($value)
 * @mixin Eloquent
 */
class NoMatchSeatRefundDetail extends RefundDetail
{

    protected $table = 'train_order_unmatched_seat_refund_details';
    protected $fillable
                     = [
            "user_id",
            "refund_sn",
            "order_sn",
            "out_trade_no",
            "trip_type",        // 出行类型
            "type",
            "name",
            "seat",
            "count",
            "amount",
        ];

    /**
     * @throws Exception
     */
    public function addMore(TripOrderRefund $refund, array $data): bool
    {
        $validator = Validator::make($data, [
            "*.user_id"      => "required",
            "*.refund_sn"    => "required",
            "*.order_sn"     => "required",
            "*.out_trade_no" => "required",
            "*.type"         => "required",
            "*.name"         => "required",
            "*.amount"       => "required",
        ]);

        if ($validator->fails()) {
            throw new Exception("退款参数错误".$validator->errors()->first());
        }

        foreach ($data as $key => $detail) {
            if ($this->findOneByDetail($detail)) {
                throw new Exception("该退款记录已存在");
            }
            $data[$key]['count']      = $detail['count'] ?? 1;
            $data[$key]['created_at'] = $refund->getCreatedAt();
            $data[$key]['updated_at'] = $refund->getCreatedAt();
        }

        return $this->insert($data);
    }

    public function findOneByDetail(array $detail): ?self
    {
        return $this->findOneByOrderTypeName($detail['order_sn'], $detail['type'], $detail['name']);
    }

    public function findOneByOrderTypeName(string $order_sn, int $type, string $name): ?self
    {
        return $this->where([
            'order_sn' => $order_sn,
            'type'     => $type,
            'name'     => $name,
        ])->first();
    }

    public function addOne(TripOrderRefund $refund, array $data)
    {
        // TODO: Implement addOne() method.
    }
}